package org.flowers.unbeaten.generator.pagination;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import org.flowers.unbeaten.exception.code.BaseResponseCode;

import java.util.List;

/**
 * @description 基于LayUI数据表格封装的分页模型
 * @author RyanWang
 * @date 2021-08-13 14:43:54
 * @version 1.0
 */
@Data
public class LayPage {

    /**
     * 响应code
     */
    private Integer code;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 数据条数
     */
    private Long count;

    /**
     * 数据
     */
    private List data;

    /**
     * 构建LayUI数据表格分页模型
     * @param page
     * @return
     */
    public static LayPage buildPagination(Pagination page) {
        LayPage result = new LayPage();
        result.setCode(BaseResponseCode.SUCCESS.getCode());
        result.setMessage(BaseResponseCode.SUCCESS.getMessage());
        result.setCount(page.getTotal());
        result.setData(page.getRows());
        return result;
    }

    /**
     * 构建LayUI数据表格分页模型
     * @param page
     * @return
     */
    public static LayPage buildPagination(IPage page) {
        LayPage result = new LayPage();
        result.setCode(BaseResponseCode.SUCCESS.getCode());
        result.setMessage(BaseResponseCode.SUCCESS.getMessage());
        result.setCount(page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 构建LayUI数据表格分页模型
     * @param data
     * @return
     */
    public static LayPage buildPagination(List data) {
        LayPage result = new LayPage();
        result.setCode(BaseResponseCode.SUCCESS.getCode());
        result.setMessage(BaseResponseCode.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    /**
     * 请求失败的结果
     * @param msg
     * @return
     */
    public static LayPage failure(String msg) {
        LayPage result = new LayPage();
        result.setCode(BaseResponseCode.FAILURE.getCode());
        result.setMessage(msg);
        return result;
    }

}
